import {Util,GridMixin} from "@lowcode/jpaas-base-lib";
import {ShareDialog} from "@lowcode/jpaas-share-component";
import ScreenApi from "@/api/screen-api";
/**
 * 接收发布配置js
 */

export default {
	inject:{
		config:{
			default:()=>{
				return {};
			}
		},
		getPubParams:{
			default:()=>{
				return [] ;
			}
		},
		getPublishOptions:{
			default:()=>{
				return [] ;
			}
		},
		paramsOptions:{
			default:()=>{
				return [];
			}
		},
		urlParams:{
			default:()=>{
				return [];
			}
		},
		constantField:{
			default:()=>{
				return [];
			}
		},
        constantList:{
            default:()=>{
                return [];
            }
        },
	},
	props:{
		publishParams:{
			type:Array,
			default:()=>[]
		}
	},
    mixins: [GridMixin],
	data() {
		return {
			valueSourceDef: [
				{ value: 'param', label:this.mapI18n('parameterPassing','参数传入')},
				{ value: 'fixedVar', label:this.mapI18n('fixValue','固定值')},
				{ value: 'script', label:this.mapI18n('script','脚本')},
				{ value: 'constantVar', label:this.mapI18n('constVar','常量')},
			],
			sourceAry: [
				{ value: 'url', label:this.mapI18n('incomParam','来自传入参数')},
				{ value: 'event', label:this.mapI18n('event','来自事件')},
			],
			componentParamsDef:[],
			selectedRowKeys:[],
			//选择的需要发布的参数
			selectPublish:[],
			publishComponent:[],
			noPublish:["numerical"]
		}
	},
	computed: {
		receiveRowSelection() {
			return {
				selectedRowKeys: this.selectedRowKeys,
				onChange: this.onSelectChange,
			}
		},
	},
	created() {
		this.initSelectPublish();
		this.publishComponent = this.getPublishOptions();
        if(this.config.advancedConf.receive.publishComponent){
        	this.changeComponent(this.config.advancedConf.receive.publishComponent);
		}
		if(this.config.reportType=='map'){
			this.listParamsDef.push({
				label:this.mapI18n('mapType','地图类型'),
				value:"mapType_"
			})
		}
	},
	methods: {
		mapI18n(name, text, key) {
			return this.$ti18(name, text, "mapLang", key);
		},
		linkageChange(value){
			//清空联动数据配置
			if(!value){
				this.config.advancedConf.receive={
					publishComponent:"",
					mapping:[]
				}
				this.config.advancedConf.publish={
					paramMapping:[]
				}
			}
		},
		changeComponent(val) {
			if(!val){
				this.config.advancedConf.receive.mapping=[];
				return;
			}
			if (this.config.key === val) {
				this.config.advancedConf.receive.publishComponent = ''
				this.$notification.warning({ message: this.mapI18n('promptMessage','提示信息'), description:this.mapI18n('publishingEvent','发布事件组件不能选择自己!')})
				return
			}
			this.componentParamsDef = this.getPubParams(val)
		},
		addParams() {
			if (this.config.advancedConf.receive.type == 'url') {
				if (!this.urlParams || this.urlParams.length==0) {
					this.$notification.warning({
						message:this.mapI18n('tipInfo','提示信息'),
						description:this.mapI18n('configure','未配置传入参数，请去配置!'),
					})
					return;
				}
			}
			if (this.config.advancedConf.receive.type == 'event' && this.componentParamsDef.length==0) {
				this.$notification.warning({
					message:this.mapI18n('tipInfo','提示信息'),
					description:this.mapI18n('component','该组件未配置传入参数，请去配置!'),
				})
				return;
			}
			this.config.advancedConf.receive.mapping.push({ idx_: Util.randomId(), name: '', valueSource: '', valueDef: '' })
		},
		moveUp(idx_) {
			this.toUp(this.config.advancedConf.receive.mapping, idx_)
		},
		moveDown(idx_) {
			this.toDown(this.config.advancedConf.receive.mapping, idx_)
		},
		removeSelect(idx_) {
			this.removeRows(this.config.advancedConf.receive.mapping, idx_)
		},
		onSelectChange(selectedRowKeys) {
			this.selectedRowKeys = selectedRowKeys
		},
		openScriptQuery(record) {
            let conf = {curVm: this,zIndex:9999999, data: {initialScript:record.valueDef}}
            ShareDialog.openJavaScriptDialog(conf, function (data) {
                record.valueDef=data;
            })
		},
		publishChange(values){
			let publishParams=[];
			for (let i= 0; i < values.length; i++) {
				let param=this.publishParams.find(item => item.value ==values[i] );
				publishParams.push({
					key:param.value,
					label:param.label
				})
			}
			this.config.advancedConf.publish.paramMapping=publishParams;
		},
		initSelectPublish(){
			this.selectPublish = [];
			if (this.config.advancedConf.publish) {
				let paramMappings=this.config.advancedConf.publish.paramMapping;
				for (let i = 0; i < paramMappings.length; i++) {
					this.selectPublish.push(paramMappings[i].key);
				}
			}
			if(this.constantList.length==0){
                ScreenApi.getConstantItem().then((data) => {
                    this.constantField = this.constantField.concat(
                        data.map((item) => {
                            return { label: item.val, value: item.key }
                        })
                    )
                })
            }
		},
		typeChange(){
			this.config.advancedConf.receive.mapping=[];
		}
	},
	watch: {}
}
